/*
 * Copyright 2011 group coding kata nantes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package fr.coding.kata.nantes.bowling.sylvain;

import fr.coding.kata.nantes.bowling.commons.BowlingInterface;
import org.apache.commons.lang.StringUtils;

/**
 * This class implements the Bowling game kata (see {@link BowlingInterface}).
 *
 * @author Sylvain Leduby
 * @since 1.0
 */
public class Bowling implements BowlingInterface {

    private String line;

    /**
     * Default constructor.
     */
    public Bowling() {
        super();
    }

    @Override
    public Bowling with(final String line) {
        this.line = line;
        return this;
    }

    @Override
    public Integer call() {
        Integer total = -1;
        if (StringUtils.isNotBlank(line)) {
            final char[] rolls = line.toCharArray();
            final Score score = computeFrameScore(rolls, 0);
            total = score.total;
        }

        return total;
    }

    private Score computeFrameScore(final char[] rolls, final int offset) {
        final Score score = new Score();

        score.first = BowlingUtil.toScore(rolls[offset]);
        final boolean strike = score.first == 10;
        boolean spare = false;

        int nextOffset = offset + 1;
        if (!strike) {
            final char secondRoll = offset + 1 == rolls.length ? 0 : rolls[offset + 1];
            spare = secondRoll == '/';
            if (spare) {
                score.second = 10 - score.first;
            } else {
                score.second = BowlingUtil.toScore(secondRoll);
            }
            nextOffset++;
        }

        score.total += score.first + score.second;

        // last frame?
        if (offset == rolls.length - 3) {
            manageLast(rolls, score, strike, nextOffset);
        } else if (nextOffset < rolls.length) {
            // recurse
            final Score rest = computeFrameScore(rolls, nextOffset);
            score.total += rest.total;

            if (spare || strike) {
                score.total += rest.first;
            }
            if (strike) {
                score.total += rest.second;
                score.second = rest.first;
            }
        }

        return score;
    }

    private void manageLast(final char[] rolls, final Score score, final boolean strike, final int nextOffset) {
        // during the last frame no extra bonus is given for strikes
        if (strike) { // frame is something like: Xnn
            final char penultimate = rolls[rolls.length - 2];
            score.second = BowlingUtil.toScore(penultimate);

            final char ultimate = rolls[rolls.length - 1];
            if (ultimate == '/') {
                // end with a spare
                score.total += 10;
            } else {
                score.total += score.second;
                score.total += BowlingUtil.toScore(ultimate);
            }
        } else { // frame is something like: "n/n" or "nn"
            // recurse
            final Score rest = computeFrameScore(rolls, nextOffset);
            score.total += rest.total;
        }
    }

    private class Score {
        public int total = 0;
        public int first = 0;
        public int second = 0;

        public Score() {
            super();
        }
    }

}
